﻿Imports System.Data

Partial Class template
    Inherits System.Web.UI.Page
    'C:\Users\Ross Brasseaux\Desktop\Business\FSE3_svn\free-student-exchange\Free Student Exchange 3\Home.aspx.vb
    Private Ross As String = "[C:\Users\Ross Brasseaux\Desktop\Business\FSE3_svn\free-student-exchange\Free Student Exchange 3\App_Data\"
    Private Ross2 As String = "[C:\Users\Ross Brasseaux\Desktop\Business\Free Student Exchange 3\App_Data\"
    Private Pat As String = "[C:\Users\Darking Duck\Documents\FSE3_svn\free-student-exchange\Free Student Exchange 3\App_Data\"
    Private specDep As String = "SELECT u.UserId, u.firstname, u.lastname, b.Title, b.Author, isnull(d.Department, '') [Department], x.University " & _
                                            "FROM Post AS p INNER JOIN Book AS b ON b.id = p.id_product " & _
                                            "INNER JOIN University x on x.ID = p.id_university  " & _
                                            "INNER JOIN departments d on d.id_department = p.id_department  " & _
                                            "INNER JOIN " & _
                                            Ross & "mDB.MDF].dbo.UserProfiles AS u  " & _
                                            "ON p.id_user = u.UserId  " & _
                                            "WHERE p.product_type like @Type AND p.id_department in ("

    Private allDep As String = "SELECT p.id_user [postUserId], p.id [postId], u.firstname, u.lastname, p.id_product [productId], p.product_type [productType], b.Title, b.Author, isnull(d.Department, '') [Department], x.University " & _
                                            "FROM Post AS p INNER JOIN Book AS b ON b.id = p.id_product " & _
                                            "INNER JOIN University x on x.ID = p.id_university  " & _
                                            "INNER JOIN departments d on d.id_department = p.id_department  " & _
                                            "INNER JOIN " & _
                                            Ross & "mDB.MDF].dbo.UserProfiles AS u  " & _
                                            "ON p.id_user = u.UserId  " & _
                                            "WHERE p.product_type like @Type AND p.id_department like '%'"
    Private addFav As String = "insert into [Favorite] (id_user, id_favorite, favType) values (@UserId, @favId, @favType)"

    Protected Sub filterButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles filterButton.Click

        'This script combined with the autopost back attribute in the listbox is supposed to check each item to see if it is
        ' selected, and then count them, and change the text of the label to the item text if it is just one, or say what the
        ' number is if it is more than one. (e.g., "3 classes selected.") But it is recognizing only the last selected item for some reason.

        Dim iCount As Integer 'Define a variable called "iCount" of type "Integer"
        Dim i As Integer, j As Integer, k As Integer
        Dim depArray() As String, depJoin As String 'Define an array as a string with no beginning and ending indices

        i = 0
        j = 0
        k = 0

        depJoin = ""

        iCount = 0 'Set iCount to zero
        For Each item As ListItem In dep_ddb.Items 'Defines the item variable for this loop and loops through the dep_ddb.Items collection
            If item.Selected Then
                iCount = iCount + 1
            End If
        Next

        If iCount > 1 Then
            ' j = 0
            'ReDim classes(0 To iCount - 1)
            'For i = class_ddb.SelectedIndex To class_ddb.Items.Count - 1
            '                If class_ddb.Items(i).Selected = True Then
            '                classes(j) = class_ddb.Items(i).Text
            'j = j + 1
            'End If
            'Next
            dep_label.Text = iCount & " departments selected"
            ReDim depArray(0 To iCount - 1)

            k = 0
            For i = 0 To iCount - 1 'After counting the number of selected items, this loops once for each selected item that was counted.
                'Each loop, the following code should find the next selected item in the list and assign it the next array;
                For j = k To dep_ddb.Items.Count - 1    'This loops through each item in the listbox (selected or not);
                    If dep_ddb.Items.Item(j).Selected = True Then   'If the item is selected;
                        depArray(i) = dep_ddb.Items.Item(j).Value   'Assign it to the current array item;
                        k = j + 1   'When a selected item is found, set the k value to the next index so the loop won't start again from beginning.
                        Exit For
                    End If
                Next
            Next

            depJoin = Join(depArray, ",")  'Combine the array.

        ElseIf iCount = 1 Then
            dep_label.Text = dep_ddb.SelectedItem.Text
            depJoin = dep_ddb.SelectedItem.Value
        End If

        If iCount = 0 Then
            posts.SelectCommand = allDep
        Else
            posts.SelectCommand = specDep & depJoin & ")"
        End If


    End Sub

    Private Sub getDataset(ByVal sender As Object, ByVal e As EventArgs) Handles depdb.Load

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            dep_ddb.ClearSelection()
            posts.SelectCommand = allDep
        End If


    End Sub

    Protected Sub clearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearButton.Click
        dep_ddb.ClearSelection()
        posts.SelectCommand = allDep
        dep_label.Text = "Department"
    End Sub

    Protected Sub addFavorites(ByVal sender As Object, ByVal e As RepeaterCommandEventArgs)

        Dim queryString As String = addFav
        Dim conn As New SqlClient.SqlConnection



        'MsgBox(CType(e.CommandSource, LinkButton).CommandArgument())

        Dim argString As String = e.CommandArgument.ToString 'Pull the joined array of parameters from the link button's command argument
        Dim argArray(0 To 1) As String 'Define new string array for parameters

        'argArray = Split(argString, ";", 2, CompareMethod.Binary) 'Split the parameter array into two array items
        'Dim favId As Guid = SqlTypes.SqlGuid.Parse(argArray(0)) 'Parse/convert the string-type GUID into a GUID-data-type and assign it to favId
        'Dim favType As String = argArray(1) 'Assign the second parameter in the array (i.e., the type of favorite) to favType



        With conn
            .ConnectionString = ConfigurationManager.ConnectionStrings("FSEdepartmentsConnection").ConnectionString 'Set the connection's connectionString
        End With

        Dim cmd As New SqlClient.SqlCommand(queryString, conn) 'Define the command to insert the new favorite item
        'cmd.Parameters.Add("@UserId") =


    End Sub

End Class
